package com.caiyi.financial.nirvana.sms.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.caiyi.financial.nirvana.sms.entity.SmsUser;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.enums.SmsMsgOperatorEnum;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.querybean.CountPhoneQueryBean;
import com.caiyi.financial.nirvana.sms.querybean.SendParamsQueryBean;
import com.caiyi.financial.nirvana.sms.util.CommUtil;
import com.caiyi.financial.nirvana.sms.util.HttpClientUtil;
import com.caiyi.financial.nirvana.sms.util.SignUtil;
import com.youyu.common.utils.YyAssert;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Service
public class WebSendService {

    @Value("${spring.profiles.active}")
    private String env;

    private static Logger logger = LoggerFactory.getLogger(WebSendService.class);

    public JSONObject sendMessage(SendParamsQueryBean sendParamsQueryBean) {

        Map<String, String> params = new HashMap();
        params.put("account", sendParamsQueryBean.getAccount());
        params.put("content", sendParamsQueryBean.getContent());
        params.put("phones", sendParamsQueryBean.getPhones());
        if (StringUtils.isNotBlank(sendParamsQueryBean.getUid())) {
            params.put("uid", sendParamsQueryBean.getUid());
        }
        if (StringUtils.isNotBlank(sendParamsQueryBean.getNotifyUrl())) {
            params.put("notifyUrl", sendParamsQueryBean.getNotifyUrl());
        }
        if (StringUtils.isNotBlank(sendParamsQueryBean.getSendTime())) {
            params.put("sendTime", sendParamsQueryBean.getSendTime());
        }
        if (StringUtils.isNotBlank(sendParamsQueryBean.getUid())) {
            params.put("extend", sendParamsQueryBean.getExtend());
        }
        //获取用户信息
        String key = String.format(JedisKey.SMS_USER, sendParamsQueryBean.getAccount());
        String userJson = JedisService.get(key);
        SmsUser user = JSON.parseObject(userJson, SmsUser.class);
        if (null == user) {
            logger.info("该账号为管理员账号：{}", sendParamsQueryBean.getAccount());
            YyAssert.isFalse(true, ReturnCodeEnum.ACCOUNT_ERROR);
        }
        String sign = SignUtil.createSign(params, user.getPassword());
        params.put("sign", sign);
        String uri = CommUtil.getSendUrl(env);
        String content = HttpClientUtil.doPost(uri, JSON.toJSONString(params));
        logger.info("return : {}, requestUrl : {}", content, uri);
        JSONObject jsStr = JSONObject.parseObject(content);
        return jsStr;
    }

//    public CountPhoneQueryBean importPhone(Set<String> phoneSet) {
//        int yiDong = 0;
//        int lianTong = 0;
//        int dianXin = 0;
//        int count = phoneSet.size();
//        int unknownCount = 0;
//        StringBuilder phoneBuilder = new StringBuilder();
//        StringBuilder ydPhones = new StringBuilder();
//        StringBuilder ltPhones = new StringBuilder();
//        StringBuilder dxPhones = new StringBuilder();
//        for (String phone : phoneSet) {
//            int operator = CommUtil.getPhoneOperator(phone);
//            if (operator == SmsMsgOperatorEnum.YD.index) {
//                phoneBuilder.append(phone + ",");
//                ydPhones.append(phone + ",");
//                yiDong++;
//                continue;
//            }
//            if (operator == SmsMsgOperatorEnum.LT.index) {
//                phoneBuilder.append(phone + ",");
//                ltPhones.append(phone + ",");
//                lianTong++;
//                continue;
//            }
//            if (operator == SmsMsgOperatorEnum.DX.index) {
//                phoneBuilder.append(phone + ",");
//                dxPhones.append(phone + ",");
//                dianXin++;
//                continue;
//            }
//            unknownCount++;
//        }
//        if (StringUtils.isNotBlank(phoneBuilder.toString())) {
//            phoneBuilder = phoneBuilder.deleteCharAt(phoneBuilder.length() - 1);
//        }
//        if (StringUtils.isNotBlank(ydPhones.toString())) {
//            ydPhones = ydPhones.deleteCharAt(ydPhones.length() - 1);
//        }
//        if (StringUtils.isNotBlank(ltPhones.toString())) {
//            ltPhones = ltPhones.deleteCharAt(ltPhones.length() - 1);
//        }
//        if (StringUtils.isNotBlank(dxPhones.toString())) {
//            dxPhones = dxPhones.deleteCharAt(dxPhones.length() - 1);
//        }
//        return new CountPhoneQueryBean(yiDong, lianTong, dianXin, count, unknownCount, phoneBuilder.toString(), ydPhones.toString(), ltPhones.toString(), dxPhones.toString());
//    }

}
